package org.ee.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.ee.core.api.PageResult;
import org.ee.core.api.Result;
import org.ee.system.dto.RoleDto;
import org.ee.system.service.RoleService;
import org.ee.system.vo.RoleVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色数据接口
 *
 * @Author zlj
 * @Date 2025/8/31 23:17
 */
@RestController
@RequestMapping("/role")
@Tag(name = "角色管理", description = "角色管理模块")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页显示角色")
    public Result<PageResult<RoleVo>> getRoles(@RequestParam(value = "current") Integer current,
                                               @RequestParam(value = "size") Integer size,
                                               @RequestParam(value = "name", required = false) String name) {
        PageResult<RoleVo> pageResult = roleService.findByPage(current, size, name);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加角色")
    @PostMapping("/add")
    public Result<Void> add(RoleDto roleDto) {
        try {
            roleService.saveOrUpdate(roleDto);
            return Result.success();
        } catch (Exception e) {
            return Result.failure(e.getMessage());
        }
    }

    @Operation(summary = "删除")
    @DeleteMapping("/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Long id) {
        try{
            roleService.delete(id);
            return Result.success();
        }catch (Exception e){
            return Result.failure(e.getMessage());
        }
    }

    @Operation(summary = "获取全部角色")
    @GetMapping("/all")
    public Result<List<RoleVo>> getRoles() {
        return Result.success(roleService.getRoles());
    }
}
